package com.hkfrich.system.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.hkfrich.common.constant.Constants;
import com.hkfrich.common.controller.BaseController;
import com.hkfrich.common.utils.ResultUtils;
import com.hkfrich.common.utils.SecurityUtils;
import com.hkfrich.redis.RedisUtils;
import com.hkfrich.system.domain.SysMenu;
import com.hkfrich.system.domain.SysRole;
import com.hkfrich.system.domain.SysUser;
import com.hkfrich.system.security.domain.SecurityUserDetails;
import com.hkfrich.system.service.SysMenuService;
import com.hkfrich.system.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @ClassName LoginController
 * @Desscription
 * @Author _HeiShuai
 * @Date 2022/8/16 15:10
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/system/hkfrich")
public class LoginController extends BaseController {

    Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;
    /**
         * @Author _HeiShuai
         * @Description //login
         * @Date 14:46 2022/8/18
         * @Param
         * @return
    **/
    @RequestMapping(value = "/login")
    @ResponseBody
    //权限校验@PreAuthorize("hasAuthority('system:dept:list')")
    public ResultUtils login(@RequestBody SysUser sysUser) {
        ResultUtils result = null;
        try {
            result = sysUserService.login(sysUser);
        }catch (Exception e){
            return ResultUtils.fail(e.getMessage());
        }
        return result;
    }

    /**
         * @Author _HeiShuai
         * @Description //退出登录
         * @Date 15:06 2022/8/19
         * @Param
         * @return
    **/
    @RequestMapping(value = "/logout")
    @ResponseBody
    public ResultUtils logout() {
        ResultUtils result = null;
        try {
            result = sysUserService.logout();
        }catch (Exception e){
            return ResultUtils.fail("退出失败");
        }
        return result;
    }

    /**
     * 查询菜单列表
     */
    @RequestMapping("/getRouter")
    @ResponseBody
    public ResultUtils getRouter(@RequestBody SysMenu sysMenu)
    {
        List<SysMenu> menus = sysMenuService.list();
        return ResultUtils.success(menus,"菜单列表查询成功");
    }


    /**
     * @return
     * @Author _HeiShuai
     * @Description //获取图片验证码
     * @Date 15:12 2022/8/16
     * @Param
     **/
    @RequestMapping("/getAuthCodeImg")
    public void getAuthCodeImg(HttpServletResponse response, @RequestParam(value = "userName") String userName) {
        //定义图形验证码的长和宽  码值个数  干扰圈数
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(90, 40, 4, 10);
        circleCaptcha.createCode();
        String authCode = circleCaptcha.getCode();
        RedisUtils.setValue(Constants.CAPTCHA_CODE_KEY + userName, authCode, 60);
        ServletOutputStream sos;
        try {
            sos = response.getOutputStream();
            BufferedImage codeImg = circleCaptcha.getImage();
            ImageIO.write(codeImg, "jpeg", sos);
            sos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
